import 'package:flutter/material.dart';
import 'package:flutter_architecture/config/app_config.dart';
import 'package:flutter_architecture/config/app_theme.dart';
import 'package:flutter_architecture/models/index.dart';
import 'package:flutter_architecture/viewmodels/theme_view_model.dart';
import 'package:flutter_architecture/viewmodels/user_view_model.dart';
import 'package:flutter_architecture/widgets/appbar/appbar_widget.dart';
import 'package:flutter_architecture/widgets/provider/provider_widget.dart';
import 'package:flutter_architecture/widgets/scaffold/scaffold_widget.dart';
import 'package:flutter_architecture/widgets/state/multistate_widget.dart';
import 'package:provider/provider.dart';
import 'package:dio_log/http_log_list_widget.dart';

/// 我的模块  我的 page
/// @author: dingwen
/// @date: 2020/12/28

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  @override
  Widget build(BuildContext context) {
    //actions
    List<Widget> actions = [
      IconButton(
          icon: Icon(Icons.message),
          onPressed: () {
            Navigator.of(context).push(
              MaterialPageRoute(
                builder: (context) => HttpLogListWidget(),
              ),
            );
          })
    ];
    return architectureScaffold(
        appBar: architectureAppBar(
            title: 'Mine\t\tPage', isShowBackButton: false, actions: actions),
        body: Column(
          children: [
            Container(
              child: Center(
                child: ProviderWidget<UserViewModel>(
                  model: UserViewModel(),
                  fetchData: (model) {
                    Map data = {'canton': '530000'};
                    Map queryParams = {'pageNum': 1, 'pageSize': 100};
//              model.fetchData(data: data, queryParams: queryParams);
                  },
                  builder: (context, model, child) {
                    return MultiStateWidget(
                      builder: (context) => _buildListView(model),
                      state: model.state,
                    );
                  },
                ),
              ),
            ),
            RaisedButton(onPressed: (){
              AppConfig.checkAppUpdate();
            }
            ,child: Text('update'),)
          ],
        ),
        floatingActionButton: FloatingActionButton(
            tooltip: 'theme switch',
            child: Text('切换主题'),
            onPressed: () => Provider.of<ThemeViewModel>(context, listen: false)
                .changeTheme()));
  }
}

///构建ListView
ListView _buildListView(UserViewModel model) {
  return ListView.builder(
    itemBuilder: (context, index) =>
        _buildUserList(context, index, model.userList),
    itemCount: model.userList.length,
  );
}

///构建Text
_buildUserList(BuildContext context, int index, List<UserModel> userList) {
  return Text(userList[index].toJson().toString());
}
